package main

import "fmt"

func main() {
	var a [2]string

	a[0] = "hello"
	a[1] = "world"

	fmt.Println(a)

	arr := [6]int{1, 2, 3, 5, 5, 6}

	fmt.Println(arr)

	// 切片方式使用数字
	fmt.Println(arr[0:2])
	fmt.Println(arr[1:3])

	//修改切片会直接修改原始数组
	sub := arr[1:4]
	fmt.Println(sub)

	sub[2] = 1e3
	fmt.Println(arr)

	//切片文法
	q := []int{2, 3, 5, 7, 11, 13}
	fmt.Println(q)

	r := []bool{true, false, true, true, false, true}
	fmt.Println(r)

	/// 这种语法可以直接创建匿名结构体数组
	s := []struct {
		i int
		b bool
	}{
		{2, true},
		{3, false},
		{5, true},
		{7, true},
		{11, false},
		{13, true},
	}
	fmt.Println(s)

	defaultList()

	lengthAndSize()

	zeroValue()
}
func defaultList() {

	// 边界可省略
	s := []int{2, 3, 5, 7, 11, 13}

	s = s[1:4]
	fmt.Println(s)

	s = s[:2]
	fmt.Println(s)

	s = s[1:]

	fmt.Println(s)

	s = []int{2, 3, 5, 7, 11, 13}

	arr := s[:]

	fmt.Println(arr)
}

func lengthAndSize() {
	s := []int{2, 3, 5, 7, 11, 13}
	PrintSlice(s)

	// Slice the slice to give it zero length.
	s = s[:1]
	PrintSlice(s)

	// Extend its length.
	s = s[:4]
	PrintSlice(s)

	// Drop its first two values.
	s = s[2:]
	PrintSlice(s)

	// 根据实验来看,如果宽度够 在切片的基础上进行 如果不够就会扩展切片边界,当原数组也不够时,报错

	s = s[:4]
	PrintSlice(s)
}
func PrintSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

func zeroValue() {

	// 数组的零值是nil
	var s []int
	fmt.Println(s, len(s), cap(s))
	if s == nil {
		fmt.Println("nil!")
	}
}
